{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "from sklearn import datasets\n",
    "import random\n",
    "import sys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "##########################\n",
    "### Required Functions ###\n",
    "##########################\n",
    "delta = 1.0\n",
    "Reg_param = 1.0\n",
    "C_param = 0.1\n",
    "batch_size = 32\n",
    "num_steps = 1000\n",
    "is_evaluation = True\n",
    "initial_learning_rate=0.1\n",
    "\n",
    "def loss_fn(W,b,x_data,y_target):\n",
    "    logits = tf.subtract(tf.matmul(x_data, W),b)\n",
    "    norm_term = tf.divide(tf.reduce_sum(tf.multiply(tf.transpose(W),W)),2)\n",
    "    classification_loss = tf.reduce_mean(tf.maximum(0., tf.subtract(delta, tf.multiply(logits, y_target))))\n",
    "    total_loss = tf.add(tf.multiply(C_param,classification_loss), tf.multiply(Reg_param,norm_term))\n",
    "    return total_loss\n",
    "\n",
    "def inference_fn(W,b,x_data,y_target):\n",
    "    prediction = tf.sign(tf.subtract(tf.matmul(x_data, W), b))\n",
    "    accuracy = tf.reduce_mean(tf.cast(tf.equal(prediction, y_target), tf.float32))\n",
    "    return accuracy\n",
    "\n",
    "def next_batch_fn(x_train,y_train,num_samples=batch_size):\n",
    "    index = np.random.choice(len(x_train), size=num_samples)\n",
    "    X_batch = x_train[index]\n",
    "    y_batch = np.transpose([y_train[index]])\n",
    "    return X_batch, y_batch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "##########################\n",
    "### Dataset peparation ###\n",
    "##########################\n",
    "\n",
    "# Dataset loading and organizing.\n",
    "iris = datasets.load_iris()\n",
    "\n",
    "# Only the first two features are extracted and used.\n",
    "X = iris.data[:, :2]\n",
    "\n",
    "# The labels are transformed to -1 and 1.\n",
    "y = np.array([1 if label==0 else -1 for label in iris.target])\n",
    "\n",
    "# Get the indices for train and test sets.\n",
    "my_randoms = np.random.choice(X.shape[0], X.shape[0], replace=False)\n",
    "train_indices = my_randoms[0:int(0.5 * X.shape[0])]\n",
    "test_indices = my_randoms[int(0.5 * X.shape[0]):]\n",
    "\n",
    "# Splitting train and test sets.\n",
    "x_train = X[train_indices]\n",
    "y_train = y[train_indices]\n",
    "x_test = X[test_indices]\n",
    "y_test = y[test_indices]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#############################\n",
    "### Defining Placeholders ###\n",
    "#############################\n",
    "\n",
    "x_data = tf.placeholder(shape=[None, X.shape[1]], dtype=tf.float32)\n",
    "y_target = tf.placeholder(shape=[None, 1], dtype=tf.float32)\n",
    "W = tf.Variable(tf.random_normal(shape=[X.shape[1],1]))\n",
    "bias = tf.Variable(tf.random_normal(shape=[1,1]))\n",
    "\n",
    "# Calculation of loss and accuracy.\n",
    "total_loss = loss_fn(W, bias, x_data, y_target)\n",
    "accuracy = inference_fn(W, bias, x_data, y_target)\n",
    "\n",
    "# Defining train_op\n",
    "train_op = tf.train.GradientDescentOptimizer(initial_learning_rate).minimize(total_loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /home/sina/anaconda/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/util/tf_should_use.py:133: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.\n",
      "Instructions for updating:\n",
      "Use `tf.global_variables_initializer` instead.\n"
     ]
    }
   ],
   "source": [
    "###############\n",
    "### Session ###\n",
    "###############\n",
    "sess = tf.Session()\n",
    "\n",
    "# Initialization of the variables.\n",
    "init = tf.initialize_all_variables()\n",
    "sess.run(init)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Step #100, training accuracy= % 92.00, testing accuracy= % 92.00 \n",
      "Step #200, training accuracy= % 98.67, testing accuracy= % 94.67 \n",
      "Step #300, training accuracy= % 98.67, testing accuracy= % 94.67 \n",
      "Step #400, training accuracy= % 98.67, testing accuracy= % 100.00 \n",
      "Step #500, training accuracy= % 98.67, testing accuracy= % 100.00 \n",
      "Step #600, training accuracy= % 98.67, testing accuracy= % 100.00 \n",
      "Step #700, training accuracy= % 98.67, testing accuracy= % 100.00 \n",
      "Step #800, training accuracy= % 98.67, testing accuracy= % 100.00 \n",
      "Step #900, training accuracy= % 98.67, testing accuracy= % 100.00 \n",
      "Step #1000, training accuracy= % 98.67, testing accuracy= % 100.00 \n"
     ]
    }
   ],
   "source": [
    "###############################\n",
    "### Training the Linear SVM ###\n",
    "###############################\n",
    "for step_idx in range(num_steps):\n",
    "\n",
    "    # Get the batch of data.\n",
    "    X_batch, y_batch = next_batch_fn(x_train, y_train, num_samples=batch_size)\n",
    "\n",
    "    # Run the optimizer.\n",
    "    sess.run(train_op, feed_dict={x_data: X_batch, y_target: y_batch})\n",
    "\n",
    "    # Calculation of loss and accuracy.\n",
    "    loss_step = sess.run(total_loss, feed_dict={x_data: X_batch, y_target: y_batch})\n",
    "    train_acc_step = sess.run(accuracy, feed_dict={x_data: x_train, y_target: np.transpose([y_train])})\n",
    "    test_acc_step = sess.run(accuracy, feed_dict={x_data: x_test, y_target: np.transpose([y_test])})\n",
    "\n",
    "    # Displaying the desired values.\n",
    "    if (step_idx + 1) % 100 == 0:\n",
    "        print('Step #%d, training accuracy= %% %.2f, testing accuracy= %% %.2f ' % (step_idx + 1, float(100 * train_acc_step), float(100 * test_acc_step)))\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": false
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bgYoo44E972G1HT/U8dxnfMib2RHA/wA3u/u2dNeTarUc/zKg\ng7t3AyYBz6W6vmRx9/3u3gNoD/Qxs4J015QqcRx7IM+7mV0IbHL3pemuJR3iPP46n/uMDvnwnOT/\nAE+6+7MRdvkcOKHK7fbhbYFQ2/G7+7YDv9q7+ywgz8zapLjMpHL3rcBcYFC1oUCfe4h+7AE+72cB\nQ81sHfAUcK6Z/aXaPkE+77Ue/6Gc+4wNeTMz4BHgQ3e/P8pufwNGhV9xPx0odfeNKSsyieI5fjNr\nF94PM+tD6HxuSV2VyWFmbc2sVfjrJsAAYFW13QJ57uM59qCed3e/3d3bu3snYCTwv+5+ebXdAnne\nIb7jP5Rzn8lX15wFXAG8F56fBPgp0AHA3acAs4ALgDXALmB0GupMlniO/zvA9WZWDuwGRnow/oT5\nWOBxM6tP6B/x0+7+opldB4E/9/Ece1DPe0Q5ct6jOtxzr2UNREQCLGOna0RE5PAp5EVEAkwhLyIS\nYAp5EZEAU8iLiASYQl5EJMAU8iIiAfb/G1R57647WKUAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fe410647050>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "if is_evaluation:\n",
    "    [[w1], [w2]] = sess.run(W)\n",
    "    [[b]] = sess.run(bias)\n",
    "    x_line = [data[1] for data in X]\n",
    "\n",
    "    # Find the separator line.\n",
    "    line = []\n",
    "    line = [-w2/w1*i+b/w1 for i in x_line]\n",
    "\n",
    "    # coor_pos_list = [positive_X, positive_y]\n",
    "    # coor_neg_list = [negative_X, negative_y]\n",
    "\n",
    "    for index, data in enumerate(X):\n",
    "        if y[index] == 1:\n",
    "            positive_X = data[1]\n",
    "            positive_y = data[0]\n",
    "        elif y[index] == -1:\n",
    "            negative_X = data[1]\n",
    "            negative_y = data[0]\n",
    "        else:\n",
    "            sys.exit(\"Invalid label!\")\n",
    "    \n",
    "    # uncomment if plotting is desired!\n",
    "    # Plotting the SVM decision boundary.\n",
    "    plt.plot(positive_X, positive_y, '+', label='Positive')\n",
    "    plt.plot(negative_X, negative_y, 'o', label='Negative')\n",
    "    plt.plot(x_line, line, 'r-', label='Separator', linewidth=3)\n",
    "    plt.legend(loc='best')\n",
    "    plt.title('Linear SVM')\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
